package com.lbj.core.widget.common

import android.content.Context
import android.text.TextUtils
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.ImageView
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.databinding.DataBindingUtil
import com.lbj.core.R
import com.lbj.core.databinding.CoreCommonTitleBinding
import com.lbj.core.ext.getViewTag
import com.lbj.core.ext.setClickNoRepeat

class CommonTitle @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : RelativeLayout(context, attrs, defStyleAttr) {

    private var binding: CoreCommonTitleBinding? = null
    private var ivStubRightAdd: ImageView? = null
    private var tvStubRightText: TextView? = null

    init {
        val rootView = LayoutInflater.from(context).inflate(R.layout.core_common_title, this)
            .apply { tag = "core_common_title".getViewTag() }
        binding = DataBindingUtil.bind(rootView)

        context.theme.obtainStyledAttributes(attrs, R.styleable.CommonTitle, 0, 0).apply {
            try {
                val attrsTitle = getString(R.styleable.CommonTitle_common_title)
                val attrsIsAddShow =
                    getBoolean(R.styleable.CommonTitle_common_right_add_show, false)
                val attrsIsTextShow =
                    getBoolean(R.styleable.CommonTitle_common_right_submit_show, false)
                val attrsRightText = getString(R.styleable.CommonTitle_common_right_submit_text)

                binding?.also {
                    it.tvTitle.text = attrsTitle ?: ""
                    if (attrsIsAddShow) ivStubRightAdd = it.stubAdd.viewStub?.inflate() as ImageView
                    if (attrsIsTextShow) tvStubRightText =
                        it.stubText.viewStub?.inflate() as TextView
                    if (!TextUtils.isEmpty(attrsRightText)) {
                        tvStubRightText = it.stubText.viewStub?.inflate() as TextView
                        tvStubRightText?.text = attrsRightText
                    }
                }
            } finally {
                recycle()
            }
        }

        binding?.rlBack?.setClickNoRepeat {
            onBackListener?.onBackClick()
        }

        ivStubRightAdd?.setClickNoRepeat {
            onRightListener?.onRightClick()
        }

        tvStubRightText?.setClickNoRepeat {
            onRightListener?.onRightClick()
        }
    }

    fun setBackListener(block: () -> Unit) {
        setListener(object : OnBackListener {
            override fun onBackClick() {
                block()
            }
        })
    }

    fun setRightListener(block: () -> Unit) {
        setRightListener(object : OnRightListener {
            override fun onRightClick() {
                block()
            }
        })
    }

    // 设置左边点击事件

    private var onBackListener: OnBackListener? = null

    private fun setListener(onBackListener: OnBackListener) {
        this.onBackListener = onBackListener
    }

    interface OnBackListener {
        fun onBackClick() {}
    }

    // 设置右边点击事件

    private var onRightListener: OnRightListener? = null

    private fun setRightListener(onRightListener: OnRightListener) {
        this.onRightListener = onRightListener
    }

    interface OnRightListener {
        fun onRightClick()
    }
}